# Build the docker image for onedev agent
FROM ubuntu:22.04 AS build

ARG ARCH="amd64" PLATFORM="x86-64" BUILD_VERSION=9.3.0
ARG RELEASE_URL="${RELEASE_URL:-https://docker.lan.yunnan.ws/downloads/onedev-${BUILD_VERSION}.zip}"

COPY entrypoint-agent.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh

RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y curl unzip apt-transport-https ca-certificates software-properties-common
RUN curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add -
RUN add-apt-repository "deb [arch=${ARCH}] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
RUN apt-get -y update && apt-get -y install docker-ce
RUN curl -k -LO "${RELEASE_URL}" \
    && unzip onedev-${BUILD_VERSION}.zip && mv onedev-${BUILD_VERSION} /app \
    \
    # 清理用不上的文件
    && find /app -name "*.bat" -depth -exec rm {} \; \
    && rm -rf /app/3rdparty-licenses


# onedev agent run container
FROM ubuntu:22.04

LABEL org.opencontainers.image.url=https://gitee.com/tekintian/onedev-server

# 从构建容器拷贝文件
COPY --from=build /entrypoint.sh /entrypoint.sh
COPY --from=build /usr/bin/docker /usr/local/bin/docker
COPY --from=build /app/agent /agent

RUN sed -i 's/archive.ubuntu.com/mirrors.aliyun.com/g' /etc/apt/sources.list \
    && apt-get update \
    && apt-get install -y locales curl git git-lfs openjdk-11-jre-headless \
    && rm -rf /var/lib/apt/lists/* \
    && sed -i -e 's/# en_US.UTF-8 UTF-8/en_US.UTF-8 UTF-8/' /etc/locale.gen \
    && locale-gen

ENV LANG=en_US.UTF-8  \
    LANGUAGE=en_US:en  \
    LC_ALL=en_US.UTF-8 

WORKDIR /agent/bin
RUN ["touch", "/agent/IN_DOCKER"]
CMD ["/entrypoint.sh"]
